﻿/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.utils;

/**
    Just a small class to hold a coordinate in int form (probably should make
    the vector class generic in the future).
**/
class IntVector {
    public var x : Int;
    public var y : Int;
    
    public function new(?x = 0, ?y = 0) {
        this.x = x;
        this.y = y;
    }
    
    /**
        Returns true if both the x and y coordinates equal each other.
    **/
    public inline function equals(v : IntVector) {
        return x == v.x && y == v.y;
    }
    
    /**
        Returns the integer distance to the other point.
        NOTE: This allows for traveling along diagonals with no extra
        distance!
    **/
    public inline function getDistanceTo(v : IntVector) : Int {
        var dx = Math.abs(x - v.x);
        var dy = Math.abs(y - v.y);
        
        return Std.int(Math.max(dx, dy));
    }
    
    public inline function getTrueDistanceTo(v : IntVector) : Float {
        return Math.sqrt((x - v.x) * (x - v.x) + (y - v.y) * (y - v.y));
    }
    
    public function clone() : IntVector {
        return new IntVector(x, y);
    }
}



#if unit

import haxe.unit.TestCase;

class IntVectorTest extends haxe.unit.TestCase {
    
    var v : IntVector;
    
    public function new(){
		super();		
	}
    
    override public function setup() {
        v = new IntVector(2, -3);
    }
    
	public function testNew() {
		assertEquals(v.x, 2);
        assertEquals(v.y, -3);
	}
    
    public function testEquals() {
        assertTrue(v.equals(new IntVector(2, -3)));
        assertFalse(v.equals(new IntVector(-2, 5)));
    }
    
    public function testGetDistanceTo() {
        assertEquals(v.getDistanceTo(new IntVector(2, -4)), 1);
        assertEquals(v.getDistanceTo(new IntVector(3, -5)), 2);
        assertEquals(v.getDistanceTo(new IntVector(4, -3)), 2);
    }
    
    public function testClone() {
        var v2 = v.clone();
        assertEquals(v.x, v2.x);
        assertEquals(v.y, v2.y);
    }
}
#end